package com.itkim.work.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import com.itkim.work.domain.YzFile;
import com.itkim.work.domain.YzWorkRes;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.itkim.work.domain.YzWork;
import com.itkim.work.service.IYzWorkService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 作品管理Controller
 * 
 * @author itkim
 * @date 2024-06-06
 */
@Controller
@RequestMapping("/work/manage")
public class YzWorkController extends BaseController
{
    private String prefix = "work/manage";

    @Autowired
    private IYzWorkService yzWorkService;

    @RequiresPermissions("work:manage:view")
    @GetMapping()
    public String manage()
    {
        return prefix + "/manage";
    }

    /**
     * 查询作品管理列表
     */
    @RequiresPermissions("work:manage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YzWorkRes yzWorkRes)
    {
        startPage();
        List<YzWorkRes> list = yzWorkService.selectYzWorkList(yzWorkRes);
        return getDataTable(list);
    }

    /**
     * 导出作品管理列表
     */
    @RequiresPermissions("work:manage:export")
    @Log(title = "作品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YzWorkRes yzWorkRes)
    {
        List<YzWorkRes> list = yzWorkService.selectYzWorkList(yzWorkRes);
        ExcelUtil<YzWorkRes> util = new ExcelUtil<YzWorkRes>(YzWorkRes.class);
        return util.exportExcel(list, "作品管理数据");
    }

    /**
     * 查看作品详情
     * 在线预览功能直接用fileAddress链接直接打开就可以看
     */
    @PostMapping("/look")
    @ResponseBody
    public Map<String,Object> look(YzWorkRes yzWorkRes){
        return yzWorkService.look(yzWorkRes);
    }

    /**
     * 下载功能
     */
    @PostMapping("/download")
    public void downloadLocal(YzFile yzFile, HttpServletResponse response) throws IOException {
        String[] split = yzFile.getFileAddress().split("/");
        String path = yzFile.getFileAddress().endsWith(".pdf") ? "D:\\WorkSpace\\JAVA\\RuoYi\\file\\" + split[4] : "D:\\WorkSpace\\JAVA\\RuoYi\\file\\video\\" + split[5];
        // 读到流中
        InputStream inputStream = new FileInputStream(path);// 文件的存放路径
        response.reset();
        response.setContentType("application/octet-stream");
        String filename = new File(path).getName();
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] b = new byte[1024];
        int len;
        //从输入流中读取一定数量的字节，并将其存储在缓冲区字节数组中，读到末尾返回-1
        while ((len = inputStream.read(b)) > 0) {
            outputStream.write(b, 0, len);
        }
        inputStream.close();
    }
}
